/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.luck.pf.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class MailUtil
{
	public static ConfigUtil config;
    /** 系统邮箱配置项 */
    private final static Map<String, String> map = new HashMap<String, String>();;
    
	public MailUtil(String file) throws Exception{
		config = new ConfigUtil(file);
		map.put("login", "config/sysmail/login");
		map.put("password", "config/sysmail/password");
		map.put("server", "config/sysmail/server");
		map.put("fromAddr", "config/sysmail/fromAddr");
		map.put("fromName", "config/sysmail/fromName");
	}

    public static void sendSysMail(String mailTo, String title, String content) throws EmailException
    {
        HtmlEmail email = new HtmlEmail();
        DefaultAuthenticator defaultAuthenticator = new DefaultAuthenticator(map.get("login"),map.get("password")); 
        email.setAuthenticator(defaultAuthenticator);
        //SMTP Server
        email.setHostName(map.get("server"));
        email.setAuthentication(map.get("login"), map.get("password"));

        try
        {
            //收件人
            email.addTo(mailTo, "");

            //系统发件人
            email.setFrom(map.get("fromAddr"), map.get("fromName"));
            
            //编码
            email.setCharset(map.get("charset"));

            //主题
            email.setSubject(title);

            //内容
            email.setHtmlMsg(content);

            email.send();
        }
        catch(EmailException e)
        {
            //e.printStackTrace();
            throw e;
        }
    }

    public static void main(String[] args) throws Exception
    {
        sendSysMail("zli@eclink.com.cn", "test", "测试邮件");
    }

}
